package com.tensquare.spit.controller;

import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Macross
 * @Type SpitController.java
 * @Desc
 * @date 2019/12/18 14:42
 */
@RestController
@RequestMapping("/spit")
@Api(description = "吐槽信息管理控制器")
public class SpitController {

    @Autowired
    private SpitService spitService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取所有的吐槽信息列表(无分页)
     * @return
     */
    @GetMapping
    @ApiOperation(value = "获取所有的吐槽信息",notes = "获取的所有信息没有进行分页处理")
    public Result<List<Spit>> findAll(){
        //获取所有的吐槽信息
        List<Spit> spits = spitService.findAll();
        //将结果进行封装并返回
        return new Result<>(true,StatusCode.OK,"成功",spits);
    }

    /**
     * 获取指定id的吐槽信息
     * @param spitId 吐槽id
     * @return
     */
    @GetMapping("/{spitId}")
    @ApiOperation(value = "获取指定的吐槽信息",notes = "获取指定吐槽ID的吐槽信息")
    public Result<Spit> findById(@PathVariable @ApiParam(value = "吐槽信息ID") String spitId){
        //获取结果并将其进行封装和返回
        return new Result<>(true, StatusCode.OK, "成功", spitService.findById(spitId));
    }

    /**
     * 根据上级ID查询吐槽信息
     * @param parentId 上级ID
     * @param page 分页页码
     * @param size 分页大小
     * @return
     */
    @GetMapping("/comment/{parentId}/{page}/{size}")
    @ApiOperation(value = "根据上级ID查询吐槽信息")
    public Result<PageResult<Spit>> findByParentId(@PathVariable @ApiParam(value = "上级ID") String parentId,
                                                   @PathVariable @ApiParam(value = "分页页码") Integer page,
                                                   @PathVariable @ApiParam(value = "分页大小") Integer size){
        //获取结果并将其进行封装和返回
        return new Result<>(true,StatusCode.OK,"查询成功",spitService.findByParentId(parentId,page,size));
    }

    /**
     * 根据查询条件查询吐槽信息列表(分页)
     * @param page 分页页码
     * @param size 分页大小
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    @ApiOperation(value = "根据查询条件查询吐槽信息列表",notes = "获取的信息进行分页处理",hidden = true)
    public Result<PageResult<Spit>> search(@RequestBody @ApiParam(value = "查询条件") Spit spit,
                                           @PathVariable @ApiParam(value = "分页页码") Integer page,
                                           @PathVariable @ApiParam(value = "分页大小") Integer size){
        //获取结果并将其进行封装和返回
        return new Result<>(true,StatusCode.OK,"成功",spitService.search(spit,page,size));
    }

    /**
     * 添加吐槽
     * @param spit 吐槽信息
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加吐槽")
    public Result addSpit(@RequestBody @ApiParam(value = "吐槽信息") Spit spit){
        //添加吐槽信息
        spitService.add(spit);
        //返回请求结果
        return new Result(true,StatusCode.OK,"成功");
    }

    /**
     * 更新吐槽
     * @param spit 吐槽信息
     * @param spitId 吐槽信息id
     * @return
     */
    @PutMapping("/{spitId}")
    @ApiOperation(value = "更新吐槽")
    public Result updateSpit(@RequestBody @ApiParam(value = "吐槽信息") Spit spit,
                             @PathVariable @ApiParam(value = "吐槽ID") String spitId){
        //绑定id
        spit.set_id(spitId);
        //更新吐槽信息
        spitService.update(spit);
        //返回请求结果
        return new Result(true,StatusCode.OK,"成功");
    }

    /**
     * 吐槽点赞,用户不能对同一个吐槽信息进行多次点赞
     * @param spitId 吐槽信息ID
     * @return
     */
    @PutMapping("/thumbup/{spitId}")
    @ApiOperation(value = "吐槽点赞",notes = "用户不能对同一个吐槽信息进行多次点赞")
    public Result thumbup(@PathVariable @ApiParam(value = "吐槽ID") String spitId){
        //这里的用户Id先写死
        String userId = "1000";
        //获取Redis中的吐槽点赞标记
        String flag = (String) redisTemplate.opsForValue().get("thumbup_" + userId + "_" + spitId);
        //判断标记是否为空
        if (StringUtils.isBlank(flag)){
            //吐槽点赞操作
            spitService.thumbup(spitId);
            //设置吐槽点赞标记
            redisTemplate.opsForValue().set("thumbup_" + userId + "_" + spitId,1);
            //返回请求结果
            return new Result(true,StatusCode.OK,"点赞成功");
        }
        //返回请求结果
        return new Result(false,StatusCode.REPERROR,"您已经点赞过了");
    }

    /**
     * 删除吐槽
     * @param spitId 吐槽信息ID
     * @return
     */
    @DeleteMapping("/{spitId}")
    @ApiOperation(value = "删除吐槽")
    public Result deleteSpit(@PathVariable @ApiParam(value = "吐槽ID") String spitId){
        //更新吐槽信息
        spitService.delete(spitId);
        //返回请求结果
        return new Result(true,StatusCode.OK,"成功");
    }

}
